package com.lingCheng.avg.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lingCheng.avg.entity.*;
import org.apache.ibatis.annotations.*;

import javax.naming.Name;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 吕继锋
 * @since 2023-09-15
 */
@Mapper
public interface AgvTasksMapper extends BaseMapper<AgvTasks>{
    @Select("select id,areaId,amrId,mapId,startPointCode,endPointCode,taskType1,taskType2,status from agv_tasks where status=0")
    List<AgvTasks> findList();

    @Update("update agv_tasks set status=#{status}*10,updateTime=now() where RwCode=#{id}")
    void updateThisRW(TaskChainPo taskChainPo);

    @Select("select RwCode from agv_tasks where RwCode=#{id}")
    AgvTasks selectByRWCode(Long id);

    @Update("update agv_tasks set Rwcode=#{rwh},status=#{status},createdTime=now()  where id=#{id}")
    void updateRWH(
            @Param("id") Long id,
            @Param("rwh") long rwh,
            @Param("status") int status);

    @Insert("INSERT INTO `wms_ksdb`.`agv_error`(`id`, `amrId`, `amrName`, `mapId`, `orderChainId`, `point`, `pos`, `warnId`, `warnName`, `warnLevel`, `time`, `updateTime`) " +
            "VALUES (#{id}, #{amrId}, #{amrName}, #{mapId}, #{orderChainId}, #{point}, #{pos}, #{warnId}, #{warnName}, #{warnLevel}, #{time}, #{updateTime})")
    void insertErrorInfo(AgvErrorInfo errorInfo);

    @Select("select * from agv_error " +
            " where amrId = #{amrId} and mapId = #{mapId} " +
            " and orderChainId = #{orderChainId} and point = #{point} " +
            " and warnId = #{warnId} and warnLevel = #{warnLevel} limit 1")

    AgvErrorInfo selectErrorInfoCount(
            @Param("amrId") long amrId,
            @Param("mapId") long mapId,
            @Param("orderChainId") long orderChainId,
            @Param("point") String point,
            @Param("warnId") String warnId,
            @Param("warnLevel") Integer warnLevel
    );

    @Update("update agv_error set updateTime = #{updateTime} where id = #{id}")
    void updateErrorInfo(AgvErrorInfo errorInfo);


    @Select("select * from agv_error_info where device_id = #{id} order by create_time desc limit 1")
    AgvErrorInfo getOneAgvErrorInfo(String id);

    @Select("select * from agv_error_info where device_id = #{id} and error_info = #{error} order by create_time desc limit 1")
    AgvErrorInfo getOneAgvErrorInfoByErrorInfo(String id);

    @Update("update agv_error_info set status = 1 , update_time = SYSDATE() where device_id = #{id}")
    int updateAgvErrorInfoById(String id);

    @Update("update agv_error_info set status = 1 , update_time = SYSDATE() where device_id = #{id} and error_info = #{error}")
    int updateAgvErrorInfoByIdAndErrorInfo(String id, String error);

    @Select("select * from agv_error_info where device_id = #{id} and status != 1")
    List<AgvErrorInfo> getAgvErrorInfoList(String id);

    @Insert("insert into agv_error_info (device_id,error_code,error_info,create_time,status,update_time) " +
            "values (#{deviceId},#{errorCode},#{errorInfo},#{createTime},#{status},#{updateTime})")
    int insertAgvErrorInfo(AgvErrorInfo errorInfo);

    @Update("UPDATE get_agvinfo SET area_id = #{areaId}, battery_percentile = #{batteryPercentile}, exception = #{exception}, exception_level = #{exceptionLevel}, " +
            "theta = #{theta}, x = #{x}, y = #{y}, map_id = #{mapId}, state = #{state}, state_id = #{stateId}, show_status = #{showStatus}, " +
            "map_name = #{mapName}, fault_rate = #{faultRate} WHERE device_id = #{deviceId};")
    int updateAgvInfo(GetAgvinfo agvinfo);

    @Insert("INSERT INTO get_agvinfo (device_id, area_id, battery_percentile, exception, exception_level, theta, x, y, " +
            "device_ip, map_id, device_name, state, state_id, show_status, map_name, fault_rate) " +
            "VALUES (#{device_id}, #{area_id}, #{battery_percentile}, #{exception}, #{exception_level}, #{theta}, #{x}, #{y}, " +
            "#{device_ip}, #{map_id}, #{device_name}, #{state}, #{state_id}, #{show_status}, #{map_name}, #{faultRate});")
    int insertAgvInfo(GetAgvinfo agvinfo);

    @Select("select * from get_agvinfo")
    List<GetAgvinfo> getAgvInfoList();

    @Select("select * from agv_error_info where DATA(createdTime) = #{toDay} and device_id = #{id}")
    List<AgvErrorInfo> getTodayAgvErrorInfoListById(@Param("id") String id,@Param("toDay") String toDay);

    @Select("select * from agv_error_info where DATA(createdTime) = #{toDay}")
    List<AgvErrorInfo> getTodayAgvErrorInfoList(@Param("toDay") String toDay);

}
